package com.iphanta.community.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iphanta.community.tags.service.TagsService;
import com.iphanta.community.user.entity.UserBasicInfo;
import com.iphanta.community.user.mapper.UserBasicInfoMapper;
import com.iphanta.community.user.service.AdminUserService;
import com.iphanta.community.user.traffic.admin.ModifyUserRequest;
import com.iphanta.community.user.traffic.admin.ModifyUserResponse;
import com.iphanta.community.user.traffic.admin.QueryUserRequest;
import com.iphanta.community.user.traffic.admin.QueryUserResponse;
import com.iphanta.community.user.traffic.admin.vo.UserItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/24/22 11:44 AM
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private UserBasicInfoMapper userBasicInfoMapper;

    @Autowired
    private TagsService tagsService;

    @Override
    public QueryUserResponse query(QueryUserRequest request) {
        if(Objects.nonNull(request.getNickname())) {
            request.setNickname(StringUtils.trim(request.getNickname()));
        }
        if(Objects.nonNull(request.getUid())) {
            request.setUid(StringUtils.trim(request.getUid()));
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<UserBasicInfo> basicInfos = this.userBasicInfoMapper.query(request.getNickname(), request.getUid());
        PageInfo<UserBasicInfo> pageInfo = new PageInfo<>(basicInfos);

        List<UserItem> list = pageInfo.getList().stream().map(e -> {
            UserItem item = new UserItem();
            item.setUid(e.getUid());
            item.setNickname(e.getNickname());
            item.setPhoto(e.getPhoto());
            item.setCreateTime(e.getCreateTime());
            item.setTags(this.tagsService.tags(e.getTagCodes()));
            return item;
        }).collect(Collectors.toList());

        QueryUserResponse response = new QueryUserResponse();
        response.setPageNum(pageInfo.getPageNum());
        response.setPageSize(pageInfo.getPageSize());
        response.setTotal(pageInfo.getTotal());
        response.setLastPage(pageInfo.getLastPage());
        response.setList(list);
        return response;
    }

    @Override
    public ModifyUserResponse modify(ModifyUserRequest request) {
        if(!StringUtils.isBlank(request.getTagCodes())) {
            this.tagsService.validCode(request.getTagCodes().split("\\|"));
            this.userBasicInfoMapper.modifyTagCodes(request.getUid(), request.getTagCodes());
        }
        ModifyUserResponse response = new ModifyUserResponse();
        response.setSuccess(true);
        return response;
    }
}
